#!/bin/bash

# copy "website" from local repository folder to Cubist to either the live, dev or user's own directory

# 1 - http://cubist.cs.washington.edu/projects/11wi/cse403/SimpleFamilyPhotos/live/
# 2 - http://cubist.cs.washington.edu/projects/11wi/cse403/SimpleFamilyPhotos/dev/
# 3 - http://cubist.cs.washington.edu/~user

echo ""
echo "This script will set up the SimpleFamilyPhotos application on the Cubist server"
echo "A Cubist username is required.  You will be asked multiple times for your Cubist password if no RSA key-pair is set up"
echo ""

read -p "What is your cubist username? " CUBISTUSER

echo ""
echo "Build targets: " 
echo "1. Live"
echo "2. Dev"
echo "3. Personal dev test" 

read -p "Choose your build target [safest choice is 3]: " BUILDTARGET

if [[ $BUILDTARGET == 1 ]]
then
	echo ""
	read -p "Are you SURE you want to update the public LIVE site? (y/n): " CONFIRMLIVE
	if [[ ! ( $CONFIRMLIVE == y* ) ]]
	then
		echo "Please start over to choose another target...exiting"
		exit 0
	fi
	HTACCESS_DIR=htaccess_fixes/live/
	REMOTE_DIR=/www/htdocs/projects/11wi/cse403/SimpleFamilyPhotos/live/
elif [[ $BUILDTARGET == 2 ]]
then
	echo ""
	read -p "Are you SURE you want to update the public DEV site? (y/n): " CONFIRMDEV
	if [[ ! ( $CONFIRMDEV == y* ) ]]
	then
		echo "Please start over to choose another target...exiting"
		exit 0
	fi
	HTACCESS_DIR=htaccess_fixes/dev/
	REMOTE_DIR=/www/htdocs/projects/11wi/cse403/SimpleFamilyPhotos/dev/
elif [[ $BUILDTARGET == 3 ]]
then
	HTACCESS_DIR=htaccess_fixes/users/$CUBISTUSER
  	REMOTE_DIR=/homes/cubist/$CUBISTUSER/www
else 
	echo "Invalid option...exiting"
	exit 0
fi


echo ""
echo "Photo options: " 
echo "1. Overwrite remote pix (local --> cubist)" 
echo "2. Update local pix (cubist --> local)"
echo "3. Leave all pix as they are"
read -p "Choose your photo option [safest choice is 3]: " SAVEPIX

cd `dirname "$0"`

if [[ $SAVEPIX == 1 ]]  # verify user wants to clobber contents of remote pix folder
then
	echo ""
	echo "This will make the remote filesystem inconsistent with the site's photo database;"
	echo "After doing this you must remove all database entries corresponding to deleted files."
	read -p "Are you SURE you want to DELETE all uploaded photos not in your local repository? (y/n): " CONFIRMDELETE
	if [[ ! ( $CONFIRMDELETE == y* ) ]]
	then
		echo "No changes made. Please start over...exiting"
		exit 0
	fi
elif [[ $SAVEPIX == 2 ]]  # replace the local pix folder with the current pix folder on cubist
then
	echo ""
	echo ">>> Copying pix changes back to local repository..."
	echo ""
	rsync -avzP --delete --exclude='phpThumb' --chmod=ugo=rwX -e ssh $CUBISTUSER@cubist.cs.washington.edu:$REMOTE_DIR/app/webroot/pix/ ../website/app/webroot/pix/
elif [[ $SAVEPIX != 3 ]]  # if SAVEPIX != 1, 2, or 3, exit
then
	echo "Invalid option...exiting"
	exit 0
fi

# sync remote site to match local repository

if [ $BUILDTARGET -ne 3 ]  # live and dev sites
then
	echo ""
	echo ">>> Removing $REMOTE_DIR"
	ssh $CUBISTUSER@cubist.cs.washington.edu "rm -r $REMOTE_DIR"
fi

if [[ $SAVEPIX == 3 ]]  # exclude pix/## folders when copying local site to cubist
then
	echo ""
	echo ">>> Copying local website (except picture folders) to Cubist..."
	echo ""
	rsync -avzP --delete --exclude='.DS_Store' --exclude='app/webroot/pix/[0-9]*' --chmod=ugo=rwX -e ssh ../website/ $HTACCESS_DIR/.htaccess $CUBISTUSER@cubist.cs.washington.edu:$REMOTE_DIR
else					# include all folders when copying local site to cubist
	echo ""
	echo ">>> Copying local website to Cubist..."
	echo ""
	rsync -avzP --delete --exclude='.DS_Store' --chmod=ugo=rwX -e ssh ../website/ $HTACCESS_DIR/.htaccess $CUBISTUSER@cubist.cs.washington.edu:$REMOTE_DIR
fi

if [ $BUILDTARGET -eq 3 ]  # personal sites
then
	echo ">>> Moving .htaccess file"
	echo ""
	# put .htaccess file where it's needed
	scp $HTACCESS_DIR/app/webroot/.htaccess $CUBISTUSER@cubist.cs.washington.edu:$REMOTE_DIR/app/webroot/
fi

echo ">>> Done copying files"
echo ""

# set up permissions and copy db info

echo ""
echo ">>> Setting permissions and updating db configuration file..."
echo ""
	
if [ $BUILDTARGET -eq 3 ]; then
	ssh $CUBISTUSER@cubist.cs.washington.edu 'cp /www/htdocs/projects/11wi/cse403/SimpleFamilyPhotos/sfp_info.php ~/sfp_info.php'
else
	ssh $CUBISTUSER@cubist.cs.washington.edu "chgrp -R cse403c $REMOTE_DIR"
fi
	ssh $CUBISTUSER@cubist.cs.washington.edu "chmod -R 775 $REMOTE_DIR"
	ssh $CUBISTUSER@cubist.cs.washington.edu "chmod -R 777 $REMOTE_DIR/app/tmp && chmod -R 777 $REMOTE_DIR/app/webroot/js && chmod -R 777 $REMOTE_DIR/app/webroot/pix"

echo ">>> Done setting permissions and updating db configuration file"
echo ""


if [ $BUILDTARGET -eq 1 ]
then
	echo "############################################################################################"
	echo "View build at: http://cubist.cs.washington.edu/projects/11wi/cse403/SimpleFamilyPhotos/live/"
	echo "############################################################################################"
	echo ""
elif [ $BUILDTARGET -eq 2 ]
then
	echo "############################################################################################"
	echo "View build at: http://cubist.cs.washington.edu/projects/11wi/cse403/SimpleFamilyPhotos/dev/"
	echo "############################################################################################"
    echo ""
elif [ $BUILDTARGET -eq 3 ]
then
	echo "##########################################################"
	echo "View build at: http://cubist.cs.washington.edu/~$CUBISTUSER"
	echo "##########################################################"
    echo ""
fi
